package javabasic.slipch.calendarseeker;

public enum Month {
	JANUARY(1,31),
	FEBRUARY(2,28),
	FEBRUARY_LEAP_YEAR(2,29),
	MARCH(3,31),
	APRIL(4,30),
	MAY(5,31),
	JUNE(6,30),
	JULY(7,31),
	AUGUST(8,31),
	SEPTEMBER(9,30),
	OCTOBER(10,31),
	NOVEMBER(11,30),
	DECEMBER(12,31);
	
	public static Month getMonth(int year, int number) {
		assert number >= 1 && number <=12;
		boolean leapYear = (year%4 == 0);
		if (number == 2 && leapYear) {
			return FEBRUARY_LEAP_YEAR;
		}
		if (number == 2 && !leapYear) {
			return FEBRUARY;
		}
		for( Month m : Month.values() ) {
			if( m.getNumber() == number ) {
				return m;
			}
		}
		throw new RuntimeException("Unreachable Code");
	}
	
	@Override 
	public String toString() {
		if (this == FEBRUARY_LEAP_YEAR) {
			return "FEBRUARY";
		}
		return super.toString();
	}
	
	static public Month first() {
		return JANUARY;
	}
	
	static public Month last() {
		return DECEMBER;
	}
	
	public Month next(int year) {
	
		if ( this == last() ) {
			throw new IllegalArgumentException();
		}
		return getMonth( year, getNumber() + 1 );
	}
	
	public Month previous(int year) {
		if (this == first()) {
			throw new IllegalArgumentException();
		}
		return getMonth( year, getNumber() - 1 );
	}
	
	public int getDaysCount() {
		return DAYS_COUNT;
	}
	
	public int getNumber() {
		return NUMBER;
	}
	
	Month( int number, int daysCount) {
		NUMBER = number;
		DAYS_COUNT = daysCount;
	}
	
	private final int NUMBER;
	private final int DAYS_COUNT;
	
}